<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'machorder.add'" ></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'machorder.delete'" ></el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search.search_keyword"  class="input-with-search" style="width: 300px" @keyup.enter="upsearch">
                        <template #prepend>
                            <el-select v-model="search.search_field" filterable clearable placeholder="请选择" style="width: 110px">
                                <el-option label="加工单号" value="purchase_process_code"/>
                                <el-option label="加工厂" value="apply_name"/>
                                <el-option label="品名" value="goods_name"/>
                                <el-option label="地区" value="ware_name"/>
                            </el-select>
                        </template>
                    </el-input>
                    <el-select v-model="search.status" placeholder="请选择状态" clearable>
                        <el-option label="未完" :value="0"></el-option>
                        <el-option label="已完成" :value="1"></el-option>
                        <el-option label="全部" :value="-1"></el-option>
                    </el-select>
                    <el-date-picker v-model="search.process_date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <!-- <el-select v-model="search.apply_id" placeholder="供应商" filterable @change="applyChange(form.apply_id)">
                        <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                    </el-select> -->
                    <!-- <el-input v-model="search.purchase_code" placeholder="关联" clearable @keyup.enter="upsearch"></el-input> -->
                    <el-select v-model="search.is_complate" placeholder="请选择状态" filterable clearable>
                        <el-option label="未完" :value="0"></el-option>
                        <el-option label="已完成" :value="1"></el-option>
                        <el-option label="全部" :value="10"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" border tableName="processTable" :column="processColumn">
                <el-table-column type="selection" width="50" align="center"></el-table-column>
                <template #purchase_process_code="{row}">
                    <div class="flex start">
                        <el-link :underline="false" type="primary" @click="table_show(row)">{{ row.purchase_process_code }}</el-link>
                        <el-icon class="icon" :size="20" @click="common.copy(row.purchase_process_code)"><el-icon-Edit /></el-icon>
                        <el-icon class="icon" color="#1e9fff" :size="20" @click="machine_bell(row)" v-auth="'machorder.message'"><el-icon-BellFilled /></el-icon>
                    </div>
                    <div>{{row.process_date}}({{row.ware_name}})</div>
                </template>
                <template #processFactory="{row}">
                    <div style="margin: 10px 0;cursor: pointer;" @click="cellSearch(row.processFactory.apply_name,'apply_name')">{{row.processFactory.apply_name}}</div>
                    <div>{{row.processFactory.linkman}}</div>
                </template>
                <template #goods_name="{row}">
                    <div>产品： <span style="color: #29f;cursor: pointer;" @click="cellSearch(row.goods_name,'goods_name')">{{row.goods_name}} </span> - {{row.process_type}}</div>
                    <div class="margin">品质： <span style="color: #29f">{{`${row.weight}克/米`}}</span></div>
                    <div class="margin">备注：<el-icon v-auth="'machorder.remark'" class="icon" color="#1e9fff" :size="18" @click="table_remark(row,)"><el-icon-EditPen /></el-icon> <span>{{row.remark}}</span></div>
                </template>
                <template #apply_name="{row}">
                    <div style="margin: 10px 0;">胚布供方： <span style="color: #29f">{{row.apply.apply_name}} </span></div>
                    <div>胚布入库单号： <span>{{row.purchase_ware_name}}</span></div>
                </template>
                <template #progress="{row}">
                    <div class="progress" @click="table_show(row)">
                        <div style="margin-bottom: 5px">{{row.deliver_num}} / {{row.total_num}}</div>
                        <el-progress style="width: 100%" :percentage="Math.round((row.deliver_num / row.total_num) * 100)" :show-text="false" />
                    </div>
                </template>
                <template #status="{row}">
                    <div class="column">
                        <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="purchase_check(row)" v-auth="'machorder.check'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                        <el-button type="primary" text size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'machorder.upload'">查看附件</el-button>
                    </div>
                </template>
                <template #createstaff="{row}">
                    <div class="flex">
                        <div>{{row.createstaff && row.createstaff.name}}</div>
                        <el-icon class="icon" :size="16" @click="purchase_staff(row)"><el-icon-UserFilled /></el-icon>
                    </div>
                </template>
                <template #operation="scope">
                    <div class="buttonFlex">
                        <el-popconfirm :title="`确定${scope.row.is_complate == 1 ? '未完' : '完成'}吗？`" @confirm="purchase_complate(scope.row)">
                            <template #reference>
                                <el-button size="small" v-auth="'machorder.complate'">{{scope.row.is_complate == 1 ? '已完成' : '未完'}}</el-button>
                            </template>
                        </el-popconfirm>
                        <el-button size="small" @click="purchase_print(scope.row)" v-auth="'machorder.print'">打印</el-button>
                        <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'machorder.edit'" v-if="scope.row.status != 1">编辑</el-button>
                        <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                            <template #reference>
                                <el-button type="danger" size="small" v-auth="'machorder.delete'" v-if="scope.row.status != 1">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                    <div class="buttonFlex">
                        <el-button size="small" @click="process_inhouse(scope.row)" v-auth="'machhouse.add'" v-if="scope.row.status == 1">入库</el-button>
                        <el-button size="small" @click="process_inrecord(scope.row)" v-auth="'machhouse.inrecord'">入库记录</el-button>
                    </div>
                    <div class="buttonFlex">
                        <el-button size="small" @click="table_diaobo(scope.row)" v-auth="'machorder.diaobo'" v-if="scope.row.need_db == 1">创建调拨单</el-button>
                    </div>
                </template>






                <!-- <el-table-column label="加工单号" prop="purchase_process_code" width="250">
                    <template #default="{row}">
                        <div class="flex start">
                            <el-link type="primary" @click="table_show(row)">{{ row.purchase_process_code }}</el-link>
                            <el-icon class="icon" :size="20" @click="common.copy(row.purchase_process_code)"><el-icon-Edit /></el-icon>
                            <el-icon class="icon" color="#1e9fff" :size="20" @click="machine_bell(row)" v-auth="'machorder.message'"><el-icon-BellFilled /></el-icon>
                        </div>
                        <div>{{row.process_date}}({{row.ware_name}})</div>
                    </template>
                </el-table-column>
                <el-table-column label="加工厂" width="150">
                    <template #default="{row}">
                        <div style="margin: 10px 0;">{{row.processFactory.apply_name}}</div>
                        <div>{{row.processFactory.linkman}}</div>
                    </template>
                </el-table-column>
                <el-table-column prop="goods_name">
                    <template #default="{row}">
                        <div>产品： <span style="color: #29f">{{row.goods_name}} </span> - {{row.process_type}}</div>
                        <div class="margin">品质： <span style="color: #29f">{{`${row.weight}克/米`}}</span></div>
                        <div class="margin">备注：<el-icon v-auth="'machorder.remark'" class="icon" color="#1e9fff" :size="18" @click="table_remark(row,)"><el-icon-EditPen /></el-icon> <span>{{row.remark}}</span></div>
                    </template>
                </el-table-column>
                <el-table-column prop="goods_name">
                    <template #default="{row}">
                        <div style="margin: 10px 0;">胚布供方： <span style="color: #29f">{{row.apply.apply_name}} </span></div>
                        <div>胚布入库单号： <span></span></div>
                    </template>
                </el-table-column>
                <el-table-column label="入库进度" align="center" width="150">
                    <template #default="{row}">
                        <div class="progress" @click="table_show(row)">
                            <div style="margin-bottom: 5px">{{row.deliver_num}} / {{row.total_num}}</div>
                            <el-progress style="width: 100%" :percentage="Math.round((row.deliver_num / row.total_num) * 100)" :show-text="false" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="审核" prop="status" width="130" align="center">
                    <template #default="{row}">
                        <div class="column">
                            <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="purchase_check(row)" v-auth="'machorder.check'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                            <el-button type="primary" text size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'machorder.upload'">查看附件</el-button>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="业务员" prop="" align="center" width="120">
                    <template #default="{row}">
                        <div class="flex">
                            <div>{{row.createstaff && row.createstaff.name}}</div>
                            <el-icon class="icon" :size="16" @click="purchase_staff(row)"><el-icon-UserFilled /></el-icon>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="280">
                    <template #default="scope">
                        <div class="buttonFlex">
                            <el-popconfirm :title="`确定${scope.row.is_complate == 1 ? '未完' : '完成'}吗？`" @confirm="purchase_complate(scope.row)">
                                <template #reference>
                                    <el-button size="small" v-auth="'machorder.complate'">{{scope.row.is_complate == 1 ? '已完成' : '未完'}}</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button size="small" @click="purchase_print(scope.row)" v-auth="'machorder.print'">打印</el-button>
                            <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'machorder.edit'" v-if="scope.row.status != 1">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="danger" size="small" v-auth="'machorder.delete'" v-if="scope.row.status != 1">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </div>
                        <div class="buttonFlex">
                            <el-button size="small" @click="process_inhouse(scope.row)" v-auth="'machhouse.add'" v-if="scope.row.status == 1">入库</el-button>
                            <el-button size="small" @click="process_inrecord(scope.row)" v-auth="'machhouse.inrecord'">入库记录</el-button>
                        </div>
                        <div class="buttonFlex">
                            <el-button size="small" @click="table_diaobo(scope.row)" v-auth="'machorder.diaobo'" v-if="scope.row.need_db == 1">创建调拨单</el-button>
                        </div>
                    </template>
                </el-table-column> -->
            </scTable>
        </el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
    <purchase-dialog v-if="dialog.purchase" ref="purchaseDialog" @success="handleSuccess" @closed="dialog.purchase = false"></purchase-dialog>
    <upload-dialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload = false"></upload-dialog>
    <notice-save v-if="dialog.notice" ref="noticeDialog" @success="handleSuccess" @closed="dialog.notice = false"></notice-save>
    <house-dialog v-if="dialog.house" ref="houseDialog" @success="handleSuccess" @closed="dialog.house = false"></house-dialog>
    <house-save v-if="dialog.housesave" ref="housesaveDialog" @success="handleSuccess" @closed="dialog.housesave = false" :query="{input_type: 3,process_id: process_id}"></house-save><!-- 入库记录 -->
    
    <diaobo-save v-if="dialog.diaobo" ref="diaoboDialog" @success="handleSuccess" @closed="dialog.diaobo = false"></diaobo-save>
    <remark-save v-if="dialog.remark" ref="remarkDialog" @success="handleSuccess" @closed="dialog.remark = false"></remark-save>

    <el-dialog title="打印" width="500" v-model="dialog.print_select" align-center>
        <el-form label-position="top">
            <el-form-item label="打印关联单">
                <el-select v-model="is_correlation">
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="打印胚布供方">
                <el-select v-model="is_apply">
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="打印价格"> 
                <el-select v-model="is_price">
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button type="primary" @click="open_print">打 印</el-button>
        </template>
    </el-dialog>
    <order-print v-if="dialog.print" ref="printDialog" @success="handleSuccess" @closed="dialog.print = false"></order-print>
</template>

<script>
import CheckStatus from '../../checkStatus.vue'
import uploadDialog from '../../upload.vue'
import saveDialog from './save'
import purchaseDialog from '../../purchase/purchase/save'
import NoticeSave from '../../noticeSave.vue'
import HouseDialog from '../house/save'
// import HouseSave from '../house/houseSave.vue'
import HouseSave from '../../selectHouse.vue'
import diaoboSave from '@/views/business/purchase/allot/save.vue'
import RemarkSave from '../../remarkSave.vue'

import orderPrint from './orderPrint.vue'

export default {
    name: '采购计划管理',
    components: {
        saveDialog, CheckStatus, purchaseDialog, uploadDialog, NoticeSave, HouseDialog, HouseSave, diaoboSave, RemarkSave, orderPrint
    },
    data() {
        return {
            dialog: {
                save: false,
                check: false,
                purchase: false,
                upload: false,
                look: false,
                notice: false,
                house: false,
                housesave: false,
                diaobo: false,
                remark: false,
                print_select: false,
            },
            apiObj: this.$API.business.machining.order.list,
            selection: [],
            search: {
                search_keyword: null,
                search_field: null,
                purchase_code: null,
                process_date: [this.moment().subtract(30, 'days').format('YYYY-MM-DD'), this.moment().format('YYYY-MM-DD')],
                status: null,
                is_complate: 0,
            },
            applyList: [],
            purchase_id: "",
            process_id: "",
            is_correlation: 1,
            is_price: 1,
            is_apply: 1,
            print_id: "",
            processColumn: [
                { label: "加工单号", prop: "purchase_process_code", width: "220",align: 'left'},
                { label: "加工厂", prop: "processFactory", width: "120",align: 'left'},
                { label: "", prop: "goods_name", width: "230",align: 'left'},
                { label: "地区", prop: "area.name", width: "100"},
                { label: "", prop: "apply_name", width: "200",align: 'left'},
                { label: "入库进度", prop: "progress", width: "150"},
                { label: "批号", prop: "batch_num", width: "110",align: 'left'},
                { label: "审核", prop: "status", width: "120",},
                { label: "业务员", prop: "createstaff", width: "230",},
                { label: "操作", prop: "operation", width: "300",fixed: 'right'},
            ],
        }
    },
    mounted() {
		this.getApply();
    },
    methods: {
        cellSearch(keyword, field){
            this.search.is_complate = 10
            this.search.search_keyword = keyword
            this.search.search_field = field
            this.$refs.table.upData(this.search)
        },
        //添加
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('show').setData(row)
            })
        },
        //删除
        async table_del(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.machining.order.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        async table_del_all(delete_ids) {
            var reqData = { id: delete_ids }
            var res = await this.$API.business.machining.order.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //批量删除
        async batch_del() {
            var delete_ids = [];
            this.selection.forEach((item) => {
                delete_ids.push(item.id)
            })

            this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                type: 'warning'
            }).then(() => {
                this.table_del_all(delete_ids)
            }).catch(() => {

            })
        },
        // 完成状态切换
        async purchase_complate(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.machining.order.complate.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("操作成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.search.is_complate = 0
            this.search.process_date = [this.moment().subtract(30, 'days').format('YYYY-MM-DD'), this.moment().format('YYYY-MM-DD')],
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess(data, mode) {
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            } else if (mode == 'machining') {
                this.$refs.table.refresh()
            }
        },
        // 供应商列表
		async getApply() {
			var res = await this.$API.basic.apply.all.get();
			this.applyList = res.data
		},
        // 加工单审核
        purchase_check(row){
            this.dialog.check = true
            this.$nextTick(() => {
                this.$refs.checkDialog.open('machining').setData(row,row.checkstaff?.name)
            })
        },
        // 查看文件
        purchase_upload(row){
            this.dialog.upload = true
            this.$nextTick(() => {
                this.$refs.uploadDialog.open('show').setData(row)
            })
        },
        // 查看采购单
        table_show_purchase(row){
            let query = {
                id: row.purchase_id,
                purchase_code: row.purchase.purchase_code
            }
            this.dialog.purchase = true
            this.$nextTick(() => {
                this.$refs.purchaseDialog.open('show').setData(query)
            })
        },
        // 入库提醒
        machine_bell(row){
            let data = {
                id: row.id,
                code: row.purchase_process_code,
                apply_id: row.apply_id
            }
            this.dialog.notice = true
            this.$nextTick(() => {
                this.$refs.noticeDialog.open('edit').setData(data,'加工单',4)
            })
        },
        // 入库
        process_inhouse(row){
            this.dialog.house = true
            this.$nextTick(() => {
                this.$refs.houseDialog.open('add').getData(row)
            })
        },
        process_inrecord(row){
            this.process_id = row.id
            this.dialog.housesave = true
            this.$nextTick(() => {
                this.$refs.housesaveDialog.open('edit')
            })
        },

        // 加工单备注
        table_remark(row){
            this.dialog.remark = true
            this.$nextTick(() => {
                this.$refs.remarkDialog.open('machining').setData(row)
            })
        },

        // 调拨
        table_diaobo(row){
            this.dialog.diaobo = true
            this.$nextTick(() => {
                this.$refs.diaoboDialog.open('edit').machinData(row)
            })
        },

        // 打印选择弹窗
        purchase_print(row){
            this.common.copy(row.purchase_process_code)
            this.print_id = row.id;
            this.dialog.print_select = true;
        },
        // 确定打印
        open_print(){
            let query = {
                id: this.print_id,
                is_correlation: this.is_correlation,
                is_price: this.is_price,
                is_apply: this.is_apply,
            }
            this.dialog.print_select = false;
            this.dialog.print = true;
            this.$nextTick(() => { 
                this.$refs.printDialog.open('print').setData(query)
            })
            this.is_correlation = 1
            this.is_price = 1
            this.is_apply = 1
        },
    }
}
</script>

<style lang="scss">
.margin{
    margin: 5px 0;
}
.flex {
    display: flex;
    align-items: center;
    justify-content: center;
    .icon{
        cursor: pointer;
        margin-left: 5px;
    }
}
</style>
